package com.lhz.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.lhz.pojos.Good;
import com.lhz.services.GoodService;

@Controller
@RequestMapping("/goods")
public class GoodControll {
	@Autowired
	GoodService goodService;

	/**
	 * 跳转到商品列表
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/toGoodsSelect")
	public ModelAndView toGoodsSelect(
			@RequestParam(name="pageNow",defaultValue="1",required=false)Integer pageNow,
			@RequestParam(name="pageSize",defaultValue="10",required=false)Integer pageSize,
			String goodsName,
			Integer goodsType) {
		ModelAndView mv=new ModelAndView("GoodsSelect");
		System.err.println("goodsName:"+goodsName);
		mv.addObject("map", goodService.findGoodByCondition(pageNow,pageSize,goodsName,goodsType));
		return mv;
	}
	
	/**
	 * 查询商品详细信息
	 * @param goodsId
	 * @return
	 */
	@RequestMapping("/toUpdateGoods")
	@ResponseBody
	public Good toUpdateGoods(Integer goodsId) {
		return goodService.getGood(goodsId);
	}
	
	/**
	 * 修改商品
	 * @param goodsName
	 * @param goodsId
	 * @param goodsPrice
	 * @return
	 */
	@RequestMapping("/updateGoods")
	@ResponseBody
	public Map<String,Object> updateGoods(String goodsName,Integer goodsId,Double goodsPrice,String goodsDetail){
		return goodService.updateGoods(goodsName, goodsId, goodsPrice,goodsDetail);
	}
	
	
	/**
	 * 修改商品状态
	 * @param goodsId
	 * @param status
	 * @return
	 */
	@RequestMapping("/updateGoodsStatus")
	@ResponseBody
	public Map<String,Object> updateGoodsStatus(Integer goodsId,String status){
		return goodService.updateGoodsStatus(goodsId, status);
	}
	
	
	/**
	 * 上传商品
	 * @param file
	 * @param goodsId
	 * @return
	 */
	@RequestMapping("/uploadImg")
	@ResponseBody
	public Map<String,Object> uploadImg(MultipartFile file,Integer goodsId){
		Map<String,Object> map=new HashMap<>();
		try {
			return goodService.uploadImg(file, goodsId);
		} catch (IOException e) {
			e.printStackTrace();
		}
		map.put("result", "上传失败");
		return map;
	}
	
	/**
	 * 删除商品
	 * @param goodsId
	 * @return
	 */
	@RequestMapping("/deleteGoods")
	@ResponseBody
	public Map<String,Object> deleteGoods(Integer goodsId){
		return goodService.deleteGoods(goodsId);
	}
	
	
}
